﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS
{
    public partial class ADDCHECKUP : Form
    {
        public string bdate;
        public string chekup;
        public string empnum;
        public string empname;
        int doFlag = 0;

        string fname;
        string mname;
        string lname;

       
        public ADDCHECKUP()
        {
            InitializeComponent();
        }

        private void search_Click(object sender, EventArgs e)
        {
            ADDPRENATAL ap = new ADDPRENATAL();
            ADDVACCINE av = new ADDVACCINE();
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";
            OleDbConnection conn = new OleDbConnection(conString);
            DataSet ds = new DataSet();

            if (this.fnum.Text == "" || this.name.Text == "")
            {
                MessageBox.Show("Field(s) empty");
            }
            else { 
                //check if the person is in the FamilyMember table of the database
                conn.Open();
                    OleDbDataAdapter tagalagay = new OleDbDataAdapter("SELECT * from FamilyMember", conn);
                    tagalagay.Fill(ds); 
                    DataTable dt = ds.Tables[0];
                    int done = 0;
                    //hanapin sa result kung nasa parehong row ang input family number at name
                    foreach (DataRow dr in dt.Rows)
                    {
                        if ((int.Parse(dr["FamilyNum"].ToString()) == int.Parse(this.fnum.Text)) && (this.name.Text == dr["FirstName"].ToString()))
                        {   //kunin ang bday dahil ito'y hindi matatagpuan sa ibang table
                            if (dr["Birthday"].ToString()[4] == '/')
                            {
                                bdate = dr["Birthday"].ToString().Substring(0, 9);
                            }
                            else if (dr["Birthday"].ToString()[5] == '/')
                            {
                                bdate = dr["Birthday"].ToString().Substring(0, 10);
                            }
                            else if (dr["Birthday"].ToString()[3] == '/')
                            {
                                bdate = dr["Birthday"].ToString().Substring(0, 8);
                            }
                            fname = dr["FirstName"].ToString();
                            mname = dr["MiddleName"].ToString();
                            lname = dr["LastName"].ToString();

                            done = 1; //flag changed kasi positive
                            break; //force exit loop kasi nakita na
                        }
                    }
                    //hindi nakita dahil wala sa database
                    if(done!=1){
                        this.prompt.Text = "No Record Found. Member Does Not Exist in Record";
                    }else{ //nakita kasi
                        done = 0;
                        if(chekup=="Prenatal"){
                            //check naman ang prenatal table para malaman kung may prenatal record na
                            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * from Prenatal", conn);
                            adapter.Fill(ds);
                            DataTable dt2 = ds.Tables[0];
                            foreach (DataRow dr in dt.Rows)
                            { //may record sa prenatal table
                                if ((int.Parse(dr["FamilyNum"].ToString()) == int.Parse(this.fnum.Text)) && (this.name.Text == dr["FirstName"].ToString()))
                                {    
                                    done = 1; //flag changed kasi positive
                                    break; //force exit loop kasi nakita na
                                }
                            }
                            if (done == 1)
                            {
                                ap.familyNo=int.Parse(this.fnum.Text);
                                ap.fname=fname;
                                ap.mName=mname;
                                ap.sname=lname;
                                ap.bday=bdate;
                                ap.empname = empname;
                                ap.empnum = empnum;
                                ap.checkuptype = "Prenatal";
                                this.Visible = false;
                                ap.Visible = true;
                            }
                            else
                            {
                                ap.familyNo=int.Parse(this.fnum.Text);
                                ap.fname=fname;
                                ap.mName=mname;
                                ap.sname=lname;
                                ap.bday=bdate;
                                ap.empname = empname;
                                ap.empnum = empnum;
                                ap.checkuptype = "Prenatal";
                                ap.promptS = "No Record Found. Member has no prenatal record";
                                this.Visible = false;
                                ap.Visible = true;
                            }
                        }
                        if(chekup=="Vaccine"){
                            //check naman ang prenatal table para malaman kung may prenatal record na
                            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * from Vaccine", conn);
                            adapter.Fill(ds);
                            DataTable dt2 = ds.Tables[0];
                            foreach (DataRow dr in dt2.Rows)
                            { //may record sa prenatal table
                                if ((int.Parse(dr["FamilyNum"].ToString()) == int.Parse(this.fnum.Text)) && (this.name.Text == dr["FirstName"].ToString()))
                                {
                                    //av.mom = dr["Mother"].ToString();
                                    //MessageBox.Show(av.mom);
                                    done = 1; //flag changed kasi positive
                                    break; //force exit loop kasi nakita na
                                }
                            }
                            if (done == 1)
                            {
                                av.familyNo = int.Parse(this.fnum.Text);
                                av.fname = fname;
                                av.mName = mname;
                                av.sname = lname;
                                av.bday = bdate;
                                av.empname = empname;
                                av.empnum = empnum;
                                av.checkuptype = "Prenatal";
                                this.Visible = false;
                                av.Visible = true;
                            }
                            else
                            {
                                av.familyNo = int.Parse(this.fnum.Text);
                                av.fname = fname;
                                av.mName = mname;
                                av.sname = lname;
                                av.bday = bdate;
                                av.empname = empname;
                                av.empnum = empnum;
                                av.checkuptype = "Prenatal";
                                //av.promptS = "No Record Found. Member has no prenatal record";
                                this.Visible = false;
                                av.Visible = true;
                            }
                        }
                    }
                conn.Close();
            }
            this.Visible = false;
        }

        private void prenatalbutton_Click(object sender, EventArgs e)
        {
            this.chekup = "Prenatal";
            this.choosepanel.Visible = false;
            this.searchpanel.Visible = true;
        }

        private void vaccinebutton_Click(object sender, EventArgs e)
        {
            this.chekup = "Vaccine";
            this.choosepanel.Visible = false;
            this.searchpanel.Visible = true;
        }

        private void ADDCHECKUP_Load(object sender, EventArgs e)
        {
            this.empNumLabel.Text = empnum;
            this.employeeLabel.Text = empname;
        }
    }
}


/*
 * if (doFlag == 0) {
     if (chekup == "Prenatal") {
         //opens the connection to the database
         conn.Open();
         //try to add entry in the Vaccine table
         try
         {
             OleDbDataAdapter tagalagay = new OleDbDataAdapter("SELECT * from Prenatal where FirstName='"+this.name.Text+"'", conn);
             tagalagay.Fill(ds);
             doFlag = 0;
         }
         catch (OleDbException) {
             this.prompt.Text = "No Record Found. Member Does Not Exist";
             doFlag = 1;
         }
         conn.Close();
         if (doFlag == 0)
         {
                            
             foreach (DataRow dr in dt.Rows)
             {
                 if (int.Parse(dr["FamilyNum"].ToString()) == int.Parse(this.fnum.Text))
                 {
                     ap.familyNo = int.Parse(this.fnum.Text);
                     ap.fname = dr["FirstName"].ToString();
                     ap.mName = dr["MiddleName"].ToString();
                     ap.sname = dr["LastName"].ToString();
                     if (int.Parse(dr["FamilyNum"].ToString()) == int.Parse(this.fnum.Text))
                         {
                             if (dr["Birthday"].ToString()[4] == '/')
                             {
                                 ap.bday = dr["Birthday"].ToString().Substring(0, 9);
                             }
                             else if (dr["Birthday"].ToString()[5] == '/')
                             {
                                 ap.bday = dr["Birthday"].ToString().Substring(0, 10);
                             }
                             else if (dr["Birthday"].ToString()[3] == '/')
                             {
                                 ap.bday = dr["Birthday"].ToString().Substring(0, 8);
                             }
                         }
                  }
             }
             ap.empname = empname;
             ap.empnum = empnum;
             ap.checkuptype = "Prenatal";
             this.Visible = false;
             ap.Visible = true;
         }
     }}
     if (chekup == "Vaccine")
     {
         ADDVACCINE av = new ADDVACCINE();
         DataTable dt = ds.Tables[0];
         foreach (DataRow dr in dt.Rows)
         {
             if (dr["FirstName"].ToString() == this.name.Text)
             {
                 av.familyNo = int.Parse(this.fnum.Text);
                 av.fname = dr["FirstName"].ToString();
                 av.mName = dr["MiddleName"].ToString();
                 av.sname = dr["LastName"].ToString();
                 if (dr["Birthday"].ToString()[4] == '/')
                 {
                     av.bday = dr["Birthday"].ToString().Substring(0, 9);
                 }
                 else if (dr["Birthday"].ToString()[5] == '/')
                 {
                     av.bday = dr["Birthday"].ToString().Substring(0, 10);
                 }
                 else if (dr["Birthday"].ToString()[3] == '/')
                 {
                     av.bday = dr["Birthday"].ToString().Substring(0, 8);
                 }
             }
         }
         av.empname = empname;
         av.empnum = empnum;
         av.checkuptype = "Vaccine";
         this.Visible = false;
         av.Visible = true;
     }
 }
*/